package com.example.isometric_tower_defense;

import java.awt.*;

public class Tower {
    public int x, y;
    private int range = 100;
    private int damage = 20;
    private int fireRate = 30; // 每30帧攻击一次
    private int fireCounter = 0;
    
    public Tower(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public void update() {
        // 更新塔状态
        fireCounter++;
    }
    
    public void render(Graphics2D g, int isoX, int isoY) {
        // 绘制塔为蓝色方形
        g.setColor(Color.BLUE);
        g.fillRect(isoX - 15, isoY - 15, 30, 30);
        
        // 绘制边框
        g.setColor(Color.BLACK);
        g.drawRect(isoX - 15, isoY - 15, 30, 30);
        
        // 绘制范围圆圈（半透明）
        g.setColor(new Color(0, 0, 255, 50)); // 半透明蓝色
        g.fillOval(isoX - range/2, isoY - range/2, range, range);
        g.setColor(Color.BLUE);
        g.drawOval(isoX - range/2, isoY - range/2, range, range);
    }
    
    // 检查是否可以攻击敌人
    public boolean canAttack(Enemy enemy) {
        if (fireCounter >= fireRate) {
            // 计算塔与敌人之间的距离
            double dx = enemy.x - x;
            double dy = enemy.y - y;
            double distance = Math.sqrt(dx * dx + dy * dy);
            
            return distance <= range;
        }
        return false;
    }
    
    // 攻击敌人
    public void attack(Enemy enemy) {
        if (canAttack(enemy)) {
            enemy.setHealth(enemy.getHealth() - damage);
            fireCounter = 0; // 重置攻击计时器
        }
    }
    
    // Getters
    public int getX() {
        return x;
    }
    
    public int getY() {
        return y;
    }
}